{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ![](https://julyedu-img-public.oss-cn-beijing.aliyuncs.com/Public/img/index/logo.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pandas教程\n",
    "\n",
    "### 2018七月在线 机器学习集训营第六期 [@Julyedu.com七月在线 ](http://www.julyedu.com)\n",
    "by 褚则伟 zeweichu@gmail.com"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "pandas是一个专门用于数据分析的python library"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [Pandas](http://pandas.pydata.org/)简介\n",
    "- python数据分析library\n",
    "- 基于numpy (对ndarray的操作)\n",
    "- 有一种用python做Excel/SQL/R的感觉\n",
    "- 为什么要学习pandas?\n",
    "- pandas和机器学习的关系，数据预处理，feature engineering。\n",
    "- 适合自学的[资料](http://pandas.pydata.org/pandas-docs/stable/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 目录\n",
    "- numpy速成\n",
    "- Series\n",
    "- DataFrame\n",
    "- Index\n",
    "- 文件读写"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Numpy简介\n",
    "\n",
    "- Numpy是Python语言的一个library [numpy](http://www.numpy.org/)\n",
    "- Numpy主要支持矩阵操作和运算\n",
    "- 现在比较流行的机器学习框架（例如Tensorflow/PyTorch等等），语法都与Numpy比较接近"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Arrays/数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,2,3], [4,5,6]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.random.random((2,2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 3) (2, 2) int64 float64\n"
     ]
    }
   ],
   "source": [
    "print(a.shape, b.shape, a.dtype, b.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 2., 3.],\n",
       "       [4., 5., 6.]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.astype(np.float)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Array indexing and slicing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "a[0, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 5, 6]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:]  # 所有\n",
    "a[1:2]\n",
    "\n",
    "# a[:, 1:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### boolean indexing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False,  True],\n",
       "       [ True,  True,  True]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a > 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[a % 2 == 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[ a % 3 == 1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数学运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.08343034, 0.32968766, 0.62943126],\n",
       "       [0.33300017, 0.81392935, 0.12196973]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.random.random((2,3))\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.16686068, 2.65937532, 4.25886252],\n",
       "       [4.66600035, 6.6278587 , 6.24393946]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.08343034, 0.32968766, 0.62943126],\n",
       "       [0.33300017, 0.81392935, 0.12196973]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = a + c\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.11823344, 1.20284509, 3.86921232],\n",
       "       [1.88644628, 7.38205166, 0.80620144]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.65064031,  3.33423376,  0.        ],\n",
       "       [ 0.        ,  0.        ,  0.        ]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a / c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### boardcasting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3.41715169,  5.6484383 ,  8.14715629],\n",
       "       [ 7.66500087, 11.06964674,  8.60984864]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.77073518, 0.74450889, 0.43033519]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "c = np.random.random((1, 3))\n",
    "\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.72241386 0.73174987 0.8852146 ]\n",
      " [0.57714914 0.39168424 0.10579013]]\n",
      "[[0.55786102 0.1826665  0.02760461]]\n",
      "[[0.40300653 0.13366619 0.024436  ]\n",
      " [0.32196901 0.07154759 0.0029203 ]]\n"
     ]
    }
   ],
   "source": [
    "a + c\n",
    "\n",
    "o = np.random.random((2, 3))\n",
    "p = np.random.random((1, 3))\n",
    "\n",
    "print(o)\n",
    "print(p)\n",
    "\n",
    "print(o * p)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当操作两个array时，numpy会逐个dimension比较它们的shape，在下述情况下，两arrays会兼容和输出broadcasting结果：<br>\n",
    "\n",
    "1. 相等\n",
    "2. 其中一个为1，（进而可进行拷贝拓展已至，shape匹配）\n",
    "3. 当两个ndarray的维度不完全相同的时候，rank较小的那个ndarray会被自动在前面加上一个一维维度，直到与另一个ndarray rank相同再检查是否匹配\n",
    "\n",
    "比如求和的时候有：\n",
    "```python\n",
    "Image (3d array):  256 x 256 x 3\n",
    "Scale (1d array):              3\n",
    "Result (3d array): 256 x 256 x 3\n",
    "\n",
    "A      (4d array):  8 x 1 x 6 x 1\n",
    "B      (3d array):      7 x 1 x 5\n",
    "Result (4d array):  8 x 7 x 6 x 5\n",
    "\n",
    "A      (2d array):  5 x 4\n",
    "B      (1d array):      1\n",
    "Result (2d array):  5 x 4\n",
    "\n",
    "A      (2d array):  15 x 3 x 5\n",
    "B      (1d array):  15 x 1 x 5\n",
    "Result (2d array):  15 x 3 x 5\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 更多数学运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.02613845,  2.67373296,  0.30886402],\n",
       "       [ 0.02613845,  0.67373296,  0.30886402]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = a + c\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0174708530234389"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.05227689,  3.34746592,  0.61772804])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(a, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4.00873543,  1.00873543])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(a, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.33624514,  0.33624514])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(a, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.64063042,  2.18241133,  0.4478723 ],\n",
       "       [ 0.33997657,  0.90396753,  0.11133527]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.dot(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.64063042,  2.18241133,  0.4478723 ],\n",
       "       [ 0.33997657,  0.90396753,  0.11133527]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul(b, a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pandas数据结构Series\n",
    "\n",
    "### 2018七月在线 机器学习集训营第六期 julyedu.com"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 构造和初始化Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Series是一个一维的数据结构，下面是一些初始化Series的方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0                  7\n",
       "1            Beijing\n",
       "2               2.17\n",
       "3             -12344\n",
       "4    Happy Birthday!\n",
       "dtype: object"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series([7, 'Beijing', 2.17, -12344, 'Happy Birthday!'])\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "pandas会默认用0到n-1来作为Series的index，但是我们也可以自己指定index。index我们可以把它理解为dict里面的key。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A                  7\n",
       "B            Beijing\n",
       "C               2.17\n",
       "D             -12344\n",
       "E    Happy Birthday!\n",
       "dtype: object"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series([7, 'Beijing', 2.17, -12344, 'Happy Birthday!'],\n",
    "             index=['A', 'B', 'C', 'D', 'E'])\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "还可以用dictionary来构造一个Series，因为Series本来就是key value pairs。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Beijing      55000.0\n",
       "Shanghai     60000.0\n",
       "Shenzhen     50000.0\n",
       "Hangzhou     20000.0\n",
       "Guangzhou    25000.0\n",
       "Suzhou           NaN\n",
       "Name: price, dtype: float64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cities = {'Beijing': 55000, 'Shanghai': 60000, 'Shenzhen': 50000, 'Hangzhou': 20000, 'Guangzhou': 25000, 'Suzhou': None}\n",
    "# apts = pd.Series(cities)\n",
    "apts = pd.Series(cities, name=\"price\")\n",
    "apts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "numpy ndarray构建一个Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1.237210\n",
       "b    1.389217\n",
       "c   -2.290955\n",
       "d   -0.142304\n",
       "e    1.077533\n",
       "dtype: float64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 选择数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以像对待一个list一样对待Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Guangzhou    25000.0\n",
       "Hangzhou     20000.0\n",
       "Shanghai     60000.0\n",
       "Name: price, dtype: float64"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apts[[4,3,1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Shanghai     60000.0\n",
       "Shenzhen     50000.0\n",
       "Hangzhou     20000.0\n",
       "Guangzhou    25000.0\n",
       "Suzhou           NaN\n",
       "Name: price, dtype: float64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apts[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Beijing      55000.0\n",
       "Shanghai     60000.0\n",
       "Shenzhen     50000.0\n",
       "Hangzhou     20000.0\n",
       "Guangzhou    25000.0\n",
       "Name: price, dtype: float64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apts[:-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "为什么下面这样会拿到两个NaN呢？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Beijing           NaN\n",
       "Guangzhou     50000.0\n",
       "Hangzhou      40000.0\n",
       "Shanghai     120000.0\n",
       "Shenzhen     100000.0\n",
       "Suzhou            NaN\n",
       "Name: price, dtype: float64"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apts[1:] + apts[:-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Series就像一个dict，前面定义的index就是用来选择数据的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20000.0"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apts[\"Hangzhou\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Hangzhou    20000.0\n",
       "Beijing     55000.0\n",
       "Shenzhen    50000.0\n",
       "Name: price, dtype: float64"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apts[[\"Hangzhou\", \"Beijing\", \"Shenzhen\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Hangzhou\" in apts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Chongqing\" in apts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "比较安全的用key读取value的方法如下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apts.get(\"Chongqing\", 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面这种写法，如果key不存在，就可能会报错了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# apts[\"Chongqing\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "boolean indexing，与numpy类似。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Hangzhou     20000.0\n",
       "Guangzhou    25000.0\n",
       "Name: price, dtype: float64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apts[apts < 50000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50000.0"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apts.median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Beijing     55000.0\n",
       "Shanghai    60000.0\n",
       "Name: price, dtype: float64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apts[apts > apts.median()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面我再详细展示一下这个boolean indexing是如何工作的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Beijing      False\n",
      "Shanghai     False\n",
      "Shenzhen     False\n",
      "Hangzhou      True\n",
      "Guangzhou     True\n",
      "Suzhou       False\n",
      "Name: price, dtype: bool\n"
     ]
    }
   ],
   "source": [
    "less_than_50000 = apts < 50000\n",
    "print(less_than_50000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hangzhou     20000.0\n",
      "Guangzhou    25000.0\n",
      "Name: price, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(apts[less_than_50000])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Series元素赋值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Series的元素可以被赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Old value:  50000.0\n",
      "New value:  55000.0\n"
     ]
    }
   ],
   "source": [
    "print(\"Old value: \", apts['Shenzhen'])\n",
    "apts['Shenzhen'] = 55000\n",
    "print(\"New value: \", apts['Shenzhen'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "前面讲过的boolean indexing在赋值的时候也可以用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hangzhou     20000.0\n",
      "Guangzhou    25000.0\n",
      "Name: price, dtype: float64\n",
      "\n",
      "Hangzhou     40000.0\n",
      "Guangzhou    40000.0\n",
      "Name: price, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(apts[apts < 50000])\n",
    "print()\n",
    "apts[apts <= 50000] = 40000\n",
    "print(apts[apts < 50000])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数学运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面我们来讲一些基本的数学运算。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Beijing      27500.0\n",
       "Shanghai     30000.0\n",
       "Shenzhen     27500.0\n",
       "Hangzhou     20000.0\n",
       "Guangzhou    20000.0\n",
       "Suzhou           NaN\n",
       "Name: price, dtype: float64"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apts / 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Beijing      3.025000e+09\n",
       "Shanghai     3.600000e+09\n",
       "Shenzhen     3.025000e+09\n",
       "Hangzhou     1.600000e+09\n",
       "Guangzhou    1.600000e+09\n",
       "Suzhou                NaN\n",
       "Name: price, dtype: float64"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apts ** 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "numpy的运算可以被运用到pandsa上去"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Beijing      3.025000e+09\n",
       "Shanghai     3.600000e+09\n",
       "Shenzhen     3.025000e+09\n",
       "Hangzhou     1.600000e+09\n",
       "Guangzhou    1.600000e+09\n",
       "Suzhou                NaN\n",
       "Name: price, dtype: float64"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.square(apts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们再定义一个新的Series做加法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Beijing      300000\n",
       "Shanghai     400000\n",
       "Shenzhen     300000\n",
       "Tianjin      200000\n",
       "Guangzhou    200000\n",
       "Chongqing    150000\n",
       "dtype: int64"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cars = pd.Series({'Beijing': 300000, 'Shanghai': 400000, 'Shenzhen': 300000, \\\n",
    "                      'Tianjin': 200000, 'Guangzhou': 200000, 'Chongqing': 150000})\n",
    "cars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Beijing      5800000.0\n",
      "Chongqing          NaN\n",
      "Guangzhou    4200000.0\n",
      "Hangzhou           NaN\n",
      "Shanghai     6400000.0\n",
      "Shenzhen     5800000.0\n",
      "Suzhou             NaN\n",
      "Tianjin            NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(cars + apts * 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据结构[Dataframe](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html)\n",
    "\n",
    "### 七月在线 julyedu.com"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一个Dataframe就是一张表格，Series表示的是一维数组，Dataframe则是一个二维数组，可以类比成一张excel的spreadsheet。也可以把Dataframe当做一组Series的集合。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建一个DataFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "dataframe可以由一个dictionary构造得到。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        city  year  population\n",
      "0    Beijing  2016        2100\n",
      "1   Shanghai  2017        2300\n",
      "2  Guangzhou  2016        1000\n",
      "3   Shenzhen  2017         700\n",
      "4   Hangzhou  2016         500\n",
      "5  Chongqing  2016         500\n"
     ]
    }
   ],
   "source": [
    "data = {'city': ['Beijing', 'Shanghai', 'Guangzhou', 'Shenzhen', 'Hangzhou', 'Chongqing'],\n",
    "       'year': [2016,2017,2016,2017,2016, 2016],\n",
    "       'population': [2100, 2300, 1000, 700, 500, 500]}\n",
    "print(pd.DataFrame(data))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "columns的名字和顺序可以指定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   year       city  population\n",
      "0  2016    Beijing        2100\n",
      "1  2017   Shanghai        2300\n",
      "2  2016  Guangzhou        1000\n",
      "3  2017   Shenzhen         700\n",
      "4  2016   Hangzhou         500\n",
      "5  2016  Chongqing         500\n"
     ]
    }
   ],
   "source": [
    "print(pd.DataFrame(data, columns=['year', 'city', 'population']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       year       city  population debt\n",
      "one    2016    Beijing        2100  NaN\n",
      "two    2017   Shanghai        2300  NaN\n",
      "three  2016  Guangzhou        1000  NaN\n",
      "four   2017   Shenzhen         700  NaN\n",
      "five   2016   Hangzhou         500  NaN\n",
      "six    2016  Chongqing         500  NaN\n"
     ]
    }
   ],
   "source": [
    "frame = pd.DataFrame(data, columns = ['year', 'city', 'population', 'debt'],\n",
    "                     index = ['one', 'two', 'three', 'four', 'five', 'six'])\n",
    "print(frame)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也可以从几个Series构建一个DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Beijing      55000.0\n",
       "Shanghai     60000.0\n",
       "Shenzhen     55000.0\n",
       "Hangzhou     40000.0\n",
       "Guangzhou    40000.0\n",
       "Suzhou           NaN\n",
       "Name: price, dtype: float64"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Beijing      300000\n",
       "Shanghai     400000\n",
       "Shenzhen     300000\n",
       "Tianjin      200000\n",
       "Guangzhou    200000\n",
       "Chongqing    150000\n",
       "dtype: int64"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>apts</th>\n",
       "      <th>cars</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Beijing</th>\n",
       "      <td>55000.0</td>\n",
       "      <td>300000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chongqing</th>\n",
       "      <td>NaN</td>\n",
       "      <td>150000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Guangzhou</th>\n",
       "      <td>40000.0</td>\n",
       "      <td>200000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Hangzhou</th>\n",
       "      <td>40000.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shanghai</th>\n",
       "      <td>60000.0</td>\n",
       "      <td>400000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shenzhen</th>\n",
       "      <td>55000.0</td>\n",
       "      <td>300000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Suzhou</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tianjin</th>\n",
       "      <td>NaN</td>\n",
       "      <td>200000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              apts      cars\n",
       "Beijing    55000.0  300000.0\n",
       "Chongqing      NaN  150000.0\n",
       "Guangzhou  40000.0  200000.0\n",
       "Hangzhou   40000.0       NaN\n",
       "Shanghai   60000.0  400000.0\n",
       "Shenzhen   55000.0  300000.0\n",
       "Suzhou         NaN       NaN\n",
       "Tianjin        NaN  200000.0"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({\"apts\": apts, \"cars\": cars})\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也可以用一个list of dicts来构建DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Han</th>\n",
       "      <th>July</th>\n",
       "      <th>Zewei</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>50000</td>\n",
       "      <td>999999</td>\n",
       "      <td>1000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>8000</td>\n",
       "      <td>99999</td>\n",
       "      <td>200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Han    July  Zewei\n",
       "0  50000  999999   1000\n",
       "1   8000   99999    200"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = [{\"July\": 999999, \"Han\": 50000, \"Zewei\": 1000}, {\"July\": 99999, \"Han\": 8000, \"Zewei\": 200}]\n",
    "pd.DataFrame(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Han</th>\n",
       "      <th>July</th>\n",
       "      <th>Zewei</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>salary</th>\n",
       "      <td>50000</td>\n",
       "      <td>999999</td>\n",
       "      <td>1000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>bonus</th>\n",
       "      <td>8000</td>\n",
       "      <td>99999</td>\n",
       "      <td>200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Han    July  Zewei\n",
       "salary  50000  999999   1000\n",
       "bonus    8000   99999    200"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(data, index=[\"salary\", \"bonus\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Beijing      55000.0\n",
       "Chongqing        NaN\n",
       "Guangzhou    40000.0\n",
       "Hangzhou     40000.0\n",
       "Shanghai     60000.0\n",
       "Shenzhen     55000.0\n",
       "Suzhou           NaN\n",
       "Tianjin          NaN\n",
       "Name: apts, dtype: float64"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"apts\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>apts</th>\n",
       "      <th>cars</th>\n",
       "      <th>total_cost</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Beijing</th>\n",
       "      <td>55000.0</td>\n",
       "      <td>300000.0</td>\n",
       "      <td>5800000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chongqing</th>\n",
       "      <td>NaN</td>\n",
       "      <td>150000.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Guangzhou</th>\n",
       "      <td>40000.0</td>\n",
       "      <td>200000.0</td>\n",
       "      <td>4200000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Hangzhou</th>\n",
       "      <td>40000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shanghai</th>\n",
       "      <td>60000.0</td>\n",
       "      <td>400000.0</td>\n",
       "      <td>6400000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shenzhen</th>\n",
       "      <td>55000.0</td>\n",
       "      <td>300000.0</td>\n",
       "      <td>5800000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Suzhou</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tianjin</th>\n",
       "      <td>NaN</td>\n",
       "      <td>200000.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              apts      cars  total_cost\n",
       "Beijing    55000.0  300000.0   5800000.0\n",
       "Chongqing      NaN  150000.0         NaN\n",
       "Guangzhou  40000.0  200000.0   4200000.0\n",
       "Hangzhou   40000.0       NaN         NaN\n",
       "Shanghai   60000.0  400000.0   6400000.0\n",
       "Shenzhen   55000.0  300000.0   5800000.0\n",
       "Suzhou         NaN       NaN         NaN\n",
       "Tianjin        NaN  200000.0         NaN"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"total_cost\"] = df[\"apts\"]*100 + df[\"cars\"]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "one        Beijing\n",
      "two       Shanghai\n",
      "three    Guangzhou\n",
      "four      Shenzhen\n",
      "five      Hangzhou\n",
      "six      Chongqing\n",
      "Name: city, dtype: object\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(frame['city'])\n",
    "type(frame['city'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "one      2016\n",
      "two      2017\n",
      "three    2016\n",
      "four     2017\n",
      "five     2016\n",
      "six      2016\n",
      "Name: year, dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(frame.year)\n",
    "type(frame.year)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "loc方法可以拿到行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "year               2016\n",
      "city          Guangzhou\n",
      "population         1000\n",
      "debt                NaN\n",
      "Name: three, dtype: object\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(frame.loc['three'])\n",
    "type(frame.loc['three'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面这种方法默认用来选列而不是选行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "iloc方法可以拿到行和列，把pandas dataframe当做numpy的ndarray来操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "year              2017\n",
       "city          Shanghai\n",
       "population        2300\n",
       "debt               NaN\n",
       "Name: two, dtype: object"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.iloc[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>population</th>\n",
       "      <th>debt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>2300</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>1000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       population debt\n",
       "two          2300  NaN\n",
       "three        1000  NaN"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.iloc[1:3, 2:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### DataFrame元素赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2100"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.at[\"one\", \"population\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/zeweichu/anaconda3/envs/py36/lib/python3.6/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>city</th>\n",
       "      <th>population</th>\n",
       "      <th>debt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>2016</td>\n",
       "      <td>Beijing</td>\n",
       "      <td>2200</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>2017</td>\n",
       "      <td>Shanghai</td>\n",
       "      <td>2300</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>2016</td>\n",
       "      <td>Guangzhou</td>\n",
       "      <td>1000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>four</th>\n",
       "      <td>2017</td>\n",
       "      <td>Shenzhen</td>\n",
       "      <td>700</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>five</th>\n",
       "      <td>2016</td>\n",
       "      <td>Hangzhou</td>\n",
       "      <td>500</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>six</th>\n",
       "      <td>2016</td>\n",
       "      <td>Chongqing</td>\n",
       "      <td>500</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       year       city  population debt\n",
       "one    2016    Beijing        2200  NaN\n",
       "two    2017   Shanghai        2300  NaN\n",
       "three  2016  Guangzhou        1000  NaN\n",
       "four   2017   Shenzhen         700  NaN\n",
       "five   2016   Hangzhou         500  NaN\n",
       "six    2016  Chongqing         500  NaN"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame[\"population\"][\"one\"] = 2200\n",
    "frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>city</th>\n",
       "      <th>population</th>\n",
       "      <th>debt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>2016</td>\n",
       "      <td>Beijing</td>\n",
       "      <td>2300</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>2017</td>\n",
       "      <td>Shanghai</td>\n",
       "      <td>2300</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>2016</td>\n",
       "      <td>Guangzhou</td>\n",
       "      <td>1000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>four</th>\n",
       "      <td>2017</td>\n",
       "      <td>Shenzhen</td>\n",
       "      <td>700</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>five</th>\n",
       "      <td>2016</td>\n",
       "      <td>Hangzhou</td>\n",
       "      <td>500</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>six</th>\n",
       "      <td>2016</td>\n",
       "      <td>Chongqing</td>\n",
       "      <td>500</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       year       city  population debt\n",
       "one    2016    Beijing        2300  NaN\n",
       "two    2017   Shanghai        2300  NaN\n",
       "three  2016  Guangzhou        1000  NaN\n",
       "four   2017   Shenzhen         700  NaN\n",
       "five   2016   Hangzhou         500  NaN\n",
       "six    2016  Chongqing         500  NaN"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.at[\"one\", \"population\"] = 2300\n",
    "frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>city</th>\n",
       "      <th>population</th>\n",
       "      <th>debt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>2016</td>\n",
       "      <td>Beijing</td>\n",
       "      <td>2200</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>2017</td>\n",
       "      <td>Shanghai</td>\n",
       "      <td>2300</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>2016</td>\n",
       "      <td>Guangzhou</td>\n",
       "      <td>1000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>four</th>\n",
       "      <td>2017</td>\n",
       "      <td>Shenzhen</td>\n",
       "      <td>700</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>five</th>\n",
       "      <td>2016</td>\n",
       "      <td>Hangzhou</td>\n",
       "      <td>500</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>six</th>\n",
       "      <td>2016</td>\n",
       "      <td>Chongqing</td>\n",
       "      <td>500</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       year       city  population debt\n",
       "one    2016    Beijing        2200  NaN\n",
       "two    2017   Shanghai        2300  NaN\n",
       "three  2016  Guangzhou        1000  NaN\n",
       "four   2017   Shenzhen         700  NaN\n",
       "five   2016   Hangzhou         500  NaN\n",
       "six    2016  Chongqing         500  NaN"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.loc[\"one\", \"population\"] = 2200\n",
    "frame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以给一整列赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       year       city  population  debt\n",
      "one    2016    Beijing        2200   100\n",
      "two    2017   Shanghai        2300   100\n",
      "three  2016  Guangzhou        1000   100\n",
      "four   2017   Shenzhen         700   100\n",
      "five   2016   Hangzhou         500   100\n",
      "six    2016  Chongqing         500   100\n"
     ]
    }
   ],
   "source": [
    "frame['debt'] = 100\n",
    "print(frame)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       year       city  population  debt\n",
      "one    2016    Beijing        2200   100\n",
      "two    2017   Shanghai        2300   100\n",
      "three  2016  Guangzhou        1000   100\n",
      "four   2017   Shenzhen         700   100\n",
      "five   2016   Hangzhou         500   100\n",
      "six       0          0           0     0\n"
     ]
    }
   ],
   "source": [
    "frame.loc['six'] = 0\n",
    "print(frame)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>city</th>\n",
       "      <th>population</th>\n",
       "      <th>debt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>2016</td>\n",
       "      <td>Beijing</td>\n",
       "      <td>2200</td>\n",
       "      <td>200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>2017</td>\n",
       "      <td>Shanghai</td>\n",
       "      <td>2300</td>\n",
       "      <td>200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>2016</td>\n",
       "      <td>Guangzhou</td>\n",
       "      <td>1000</td>\n",
       "      <td>200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>four</th>\n",
       "      <td>2017</td>\n",
       "      <td>Shenzhen</td>\n",
       "      <td>700</td>\n",
       "      <td>200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>five</th>\n",
       "      <td>2016</td>\n",
       "      <td>Hangzhou</td>\n",
       "      <td>500</td>\n",
       "      <td>200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>six</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       year       city  population  debt\n",
       "one    2016    Beijing        2200   200\n",
       "two    2017   Shanghai        2300   200\n",
       "three  2016  Guangzhou        1000   200\n",
       "four   2017   Shenzhen         700   200\n",
       "five   2016   Hangzhou         500   200\n",
       "six       0          0           0   200"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.loc[:, \"debt\"] = 200\n",
    "frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "one        Beijing\n",
       "two       Shanghai\n",
       "three    Guangzhou\n",
       "four      Shenzhen\n",
       "five      Hangzhou\n",
       "six              0\n",
       "Name: city, dtype: object"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame\n",
    "frame.index\n",
    "frame['city']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       year       city  population  debt\n",
      "one    2016    Beijing        2200     0\n",
      "two    2017   Shanghai        2300     1\n",
      "three  2016  Guangzhou        1000     2\n",
      "four   2017   Shenzhen         700     3\n",
      "five   2016   Hangzhou         500     4\n",
      "six       0          0           0     5\n"
     ]
    }
   ],
   "source": [
    "frame.debt = np.arange(6)\n",
    "print(frame)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "还可以用Series来指定需要修改的index以及相对应的value，没有指定的默认用NaN."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       year       city  population   debt\n",
      "one    2016    Beijing        2200    NaN\n",
      "two    2017   Shanghai        2300  100.0\n",
      "three  2016  Guangzhou        1000  200.0\n",
      "four   2017   Shenzhen         700    NaN\n",
      "five   2016   Hangzhou         500  300.0\n",
      "six       0          0           0    NaN\n"
     ]
    }
   ],
   "source": [
    "val = pd.Series([100, 200, 300], index=['two', 'three', 'five'])\n",
    "frame['debt'] = val\n",
    "print(frame)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       year       city  population   debt  western\n",
      "one    2016    Beijing        2200    NaN    False\n",
      "two    2017   Shanghai        2300  100.0    False\n",
      "three  2016  Guangzhou        1000  200.0    False\n",
      "four   2017   Shenzhen         700    NaN    False\n",
      "five   2016   Hangzhou         500  300.0    False\n",
      "six       0          0           0    NaN    False\n"
     ]
    }
   ],
   "source": [
    "frame['western'] = (frame.city == 'Chongqing')\n",
    "print(frame)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果我们想要知道有哪些列，直接用columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['year', 'city', 'population', 'debt', 'western'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "print(frame.columns)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "行的话就叫做index啦"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['one', 'two', 'three', 'four', 'five', 'six'], dtype='object')"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.index"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一个DataFrame就和一个numpy 2d array一样，可以被转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pop = {'Beijing': {2016: 2100, 2017:2200},\n",
    "      'Shanghai': {2015:2400, 2016:2500, 2017:2600}}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <td>2016</td>\n",
       "      <td>2017</td>\n",
       "      <td>2016</td>\n",
       "      <td>2017</td>\n",
       "      <td>2016</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <td>Beijing</td>\n",
       "      <td>Shanghai</td>\n",
       "      <td>Guangzhou</td>\n",
       "      <td>Shenzhen</td>\n",
       "      <td>Hangzhou</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>population</th>\n",
       "      <td>2200</td>\n",
       "      <td>2300</td>\n",
       "      <td>1000</td>\n",
       "      <td>700</td>\n",
       "      <td>500</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>debt</th>\n",
       "      <td>NaN</td>\n",
       "      <td>100</td>\n",
       "      <td>200</td>\n",
       "      <td>NaN</td>\n",
       "      <td>300</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>western</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                one       two      three      four      five    six\n",
       "year           2016      2017       2016      2017      2016      0\n",
       "city        Beijing  Shanghai  Guangzhou  Shenzhen  Hangzhou      0\n",
       "population     2200      2300       1000       700       500      0\n",
       "debt            NaN       100        200       NaN       300    NaN\n",
       "western       False     False      False     False     False  False"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们还可以指定index的名字和列的名字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "information  year       city  population   debt  western\n",
      "index                                                   \n",
      "one          2016    Beijing        2200    NaN    False\n",
      "two          2017   Shanghai        2300  100.0    False\n",
      "three        2016  Guangzhou        1000  200.0    False\n",
      "four         2017   Shenzhen         700    NaN    False\n",
      "five         2016   Hangzhou         500  300.0    False\n",
      "six             0          0           0    NaN    False\n"
     ]
    }
   ],
   "source": [
    "frame.index.name = 'index'\n",
    "frame.columns.name = 'information'\n",
    "print(frame)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2016 'Beijing' 2200 nan False]\n",
      " [2017 'Shanghai' 2300 100.0 False]\n",
      " [2016 'Guangzhou' 1000 200.0 False]\n",
      " [2017 'Shenzhen' 700 nan False]\n",
      " [2016 'Hangzhou' 500 300.0 False]\n",
      " [0 0 0 nan False]]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "print(frame.values)\n",
    "print(type(frame.values))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## csv文件读写"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- [read_csv](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html)\n",
    "- [to_csv](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>#</th>\n",
       "      <th>Name</th>\n",
       "      <th>Type 1</th>\n",
       "      <th>Type 2</th>\n",
       "      <th>Total</th>\n",
       "      <th>HP</th>\n",
       "      <th>Attack</th>\n",
       "      <th>Defense</th>\n",
       "      <th>Sp. Atk</th>\n",
       "      <th>Sp. Def</th>\n",
       "      <th>Speed</th>\n",
       "      <th>Generation</th>\n",
       "      <th>Legendary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Bulbasaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>318</td>\n",
       "      <td>45</td>\n",
       "      <td>49</td>\n",
       "      <td>49</td>\n",
       "      <td>65</td>\n",
       "      <td>65</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Ivysaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>405</td>\n",
       "      <td>60</td>\n",
       "      <td>62</td>\n",
       "      <td>63</td>\n",
       "      <td>80</td>\n",
       "      <td>80</td>\n",
       "      <td>60</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>525</td>\n",
       "      <td>80</td>\n",
       "      <td>82</td>\n",
       "      <td>83</td>\n",
       "      <td>100</td>\n",
       "      <td>100</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>VenusaurMega Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>625</td>\n",
       "      <td>80</td>\n",
       "      <td>100</td>\n",
       "      <td>123</td>\n",
       "      <td>122</td>\n",
       "      <td>120</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>Charmander</td>\n",
       "      <td>Fire</td>\n",
       "      <td>NaN</td>\n",
       "      <td>309</td>\n",
       "      <td>39</td>\n",
       "      <td>52</td>\n",
       "      <td>43</td>\n",
       "      <td>60</td>\n",
       "      <td>50</td>\n",
       "      <td>65</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>Charmeleon</td>\n",
       "      <td>Fire</td>\n",
       "      <td>NaN</td>\n",
       "      <td>405</td>\n",
       "      <td>58</td>\n",
       "      <td>64</td>\n",
       "      <td>58</td>\n",
       "      <td>80</td>\n",
       "      <td>65</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>Charizard</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Flying</td>\n",
       "      <td>534</td>\n",
       "      <td>78</td>\n",
       "      <td>84</td>\n",
       "      <td>78</td>\n",
       "      <td>109</td>\n",
       "      <td>85</td>\n",
       "      <td>100</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6</td>\n",
       "      <td>CharizardMega Charizard X</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Dragon</td>\n",
       "      <td>634</td>\n",
       "      <td>78</td>\n",
       "      <td>130</td>\n",
       "      <td>111</td>\n",
       "      <td>130</td>\n",
       "      <td>85</td>\n",
       "      <td>100</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>6</td>\n",
       "      <td>CharizardMega Charizard Y</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Flying</td>\n",
       "      <td>634</td>\n",
       "      <td>78</td>\n",
       "      <td>104</td>\n",
       "      <td>78</td>\n",
       "      <td>159</td>\n",
       "      <td>115</td>\n",
       "      <td>100</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>7</td>\n",
       "      <td>Squirtle</td>\n",
       "      <td>Water</td>\n",
       "      <td>NaN</td>\n",
       "      <td>314</td>\n",
       "      <td>44</td>\n",
       "      <td>48</td>\n",
       "      <td>65</td>\n",
       "      <td>50</td>\n",
       "      <td>64</td>\n",
       "      <td>43</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>8</td>\n",
       "      <td>Wartortle</td>\n",
       "      <td>Water</td>\n",
       "      <td>NaN</td>\n",
       "      <td>405</td>\n",
       "      <td>59</td>\n",
       "      <td>63</td>\n",
       "      <td>80</td>\n",
       "      <td>65</td>\n",
       "      <td>80</td>\n",
       "      <td>58</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>9</td>\n",
       "      <td>Blastoise</td>\n",
       "      <td>Water</td>\n",
       "      <td>NaN</td>\n",
       "      <td>530</td>\n",
       "      <td>79</td>\n",
       "      <td>83</td>\n",
       "      <td>100</td>\n",
       "      <td>85</td>\n",
       "      <td>105</td>\n",
       "      <td>78</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>9</td>\n",
       "      <td>BlastoiseMega Blastoise</td>\n",
       "      <td>Water</td>\n",
       "      <td>NaN</td>\n",
       "      <td>630</td>\n",
       "      <td>79</td>\n",
       "      <td>103</td>\n",
       "      <td>120</td>\n",
       "      <td>135</td>\n",
       "      <td>115</td>\n",
       "      <td>78</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>10</td>\n",
       "      <td>Caterpie</td>\n",
       "      <td>Bug</td>\n",
       "      <td>NaN</td>\n",
       "      <td>195</td>\n",
       "      <td>45</td>\n",
       "      <td>30</td>\n",
       "      <td>35</td>\n",
       "      <td>20</td>\n",
       "      <td>20</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>11</td>\n",
       "      <td>Metapod</td>\n",
       "      <td>Bug</td>\n",
       "      <td>NaN</td>\n",
       "      <td>205</td>\n",
       "      <td>50</td>\n",
       "      <td>20</td>\n",
       "      <td>55</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>30</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>12</td>\n",
       "      <td>Butterfree</td>\n",
       "      <td>Bug</td>\n",
       "      <td>Flying</td>\n",
       "      <td>395</td>\n",
       "      <td>60</td>\n",
       "      <td>45</td>\n",
       "      <td>50</td>\n",
       "      <td>90</td>\n",
       "      <td>80</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>13</td>\n",
       "      <td>Weedle</td>\n",
       "      <td>Bug</td>\n",
       "      <td>Poison</td>\n",
       "      <td>195</td>\n",
       "      <td>40</td>\n",
       "      <td>35</td>\n",
       "      <td>30</td>\n",
       "      <td>20</td>\n",
       "      <td>20</td>\n",
       "      <td>50</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>14</td>\n",
       "      <td>Kakuna</td>\n",
       "      <td>Bug</td>\n",
       "      <td>Poison</td>\n",
       "      <td>205</td>\n",
       "      <td>45</td>\n",
       "      <td>25</td>\n",
       "      <td>50</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>35</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>15</td>\n",
       "      <td>Beedrill</td>\n",
       "      <td>Bug</td>\n",
       "      <td>Poison</td>\n",
       "      <td>395</td>\n",
       "      <td>65</td>\n",
       "      <td>90</td>\n",
       "      <td>40</td>\n",
       "      <td>45</td>\n",
       "      <td>80</td>\n",
       "      <td>75</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>15</td>\n",
       "      <td>BeedrillMega Beedrill</td>\n",
       "      <td>Bug</td>\n",
       "      <td>Poison</td>\n",
       "      <td>495</td>\n",
       "      <td>65</td>\n",
       "      <td>150</td>\n",
       "      <td>40</td>\n",
       "      <td>15</td>\n",
       "      <td>80</td>\n",
       "      <td>145</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     #                       Name Type 1  Type 2  Total  HP  Attack  Defense  \\\n",
       "0    1                  Bulbasaur  Grass  Poison    318  45      49       49   \n",
       "1    2                    Ivysaur  Grass  Poison    405  60      62       63   \n",
       "2    3                   Venusaur  Grass  Poison    525  80      82       83   \n",
       "3    3      VenusaurMega Venusaur  Grass  Poison    625  80     100      123   \n",
       "4    4                 Charmander   Fire     NaN    309  39      52       43   \n",
       "5    5                 Charmeleon   Fire     NaN    405  58      64       58   \n",
       "6    6                  Charizard   Fire  Flying    534  78      84       78   \n",
       "7    6  CharizardMega Charizard X   Fire  Dragon    634  78     130      111   \n",
       "8    6  CharizardMega Charizard Y   Fire  Flying    634  78     104       78   \n",
       "9    7                   Squirtle  Water     NaN    314  44      48       65   \n",
       "10   8                  Wartortle  Water     NaN    405  59      63       80   \n",
       "11   9                  Blastoise  Water     NaN    530  79      83      100   \n",
       "12   9    BlastoiseMega Blastoise  Water     NaN    630  79     103      120   \n",
       "13  10                   Caterpie    Bug     NaN    195  45      30       35   \n",
       "14  11                    Metapod    Bug     NaN    205  50      20       55   \n",
       "15  12                 Butterfree    Bug  Flying    395  60      45       50   \n",
       "16  13                     Weedle    Bug  Poison    195  40      35       30   \n",
       "17  14                     Kakuna    Bug  Poison    205  45      25       50   \n",
       "18  15                   Beedrill    Bug  Poison    395  65      90       40   \n",
       "19  15      BeedrillMega Beedrill    Bug  Poison    495  65     150       40   \n",
       "\n",
       "    Sp. Atk  Sp. Def  Speed  Generation  Legendary  \n",
       "0        65       65     45           1      False  \n",
       "1        80       80     60           1      False  \n",
       "2       100      100     80           1      False  \n",
       "3       122      120     80           1      False  \n",
       "4        60       50     65           1      False  \n",
       "5        80       65     80           1      False  \n",
       "6       109       85    100           1      False  \n",
       "7       130       85    100           1      False  \n",
       "8       159      115    100           1      False  \n",
       "9        50       64     43           1      False  \n",
       "10       65       80     58           1      False  \n",
       "11       85      105     78           1      False  \n",
       "12      135      115     78           1      False  \n",
       "13       20       20     45           1      False  \n",
       "14       25       25     30           1      False  \n",
       "15       90       80     70           1      False  \n",
       "16       20       20     50           1      False  \n",
       "17       25       25     35           1      False  \n",
       "18       45       80     75           1      False  \n",
       "19       15       80    145           1      False  "
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pokemon = pd.read_csv(\"data/Pokemon.csv\")\n",
    "pokemon.head(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>#</th>\n",
       "      <th>Name</th>\n",
       "      <th>Type 1</th>\n",
       "      <th>Type 2</th>\n",
       "      <th>Total</th>\n",
       "      <th>HP</th>\n",
       "      <th>Attack</th>\n",
       "      <th>Defense</th>\n",
       "      <th>Sp. Atk</th>\n",
       "      <th>Sp. Def</th>\n",
       "      <th>Speed</th>\n",
       "      <th>Generation</th>\n",
       "      <th>Legendary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Bulbasaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>318</td>\n",
       "      <td>45</td>\n",
       "      <td>49</td>\n",
       "      <td>49</td>\n",
       "      <td>65</td>\n",
       "      <td>65</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Ivysaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>405</td>\n",
       "      <td>60</td>\n",
       "      <td>62</td>\n",
       "      <td>63</td>\n",
       "      <td>80</td>\n",
       "      <td>80</td>\n",
       "      <td>60</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>525</td>\n",
       "      <td>80</td>\n",
       "      <td>82</td>\n",
       "      <td>83</td>\n",
       "      <td>100</td>\n",
       "      <td>100</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>VenusaurMega Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>625</td>\n",
       "      <td>80</td>\n",
       "      <td>100</td>\n",
       "      <td>123</td>\n",
       "      <td>122</td>\n",
       "      <td>120</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>Charmander</td>\n",
       "      <td>Fire</td>\n",
       "      <td>NaN</td>\n",
       "      <td>309</td>\n",
       "      <td>39</td>\n",
       "      <td>52</td>\n",
       "      <td>43</td>\n",
       "      <td>60</td>\n",
       "      <td>50</td>\n",
       "      <td>65</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   #                   Name Type 1  Type 2  Total  HP  Attack  Defense  \\\n",
       "0  1              Bulbasaur  Grass  Poison    318  45      49       49   \n",
       "1  2                Ivysaur  Grass  Poison    405  60      62       63   \n",
       "2  3               Venusaur  Grass  Poison    525  80      82       83   \n",
       "3  3  VenusaurMega Venusaur  Grass  Poison    625  80     100      123   \n",
       "4  4             Charmander   Fire     NaN    309  39      52       43   \n",
       "\n",
       "   Sp. Atk  Sp. Def  Speed  Generation  Legendary  \n",
       "0       65       65     45           1      False  \n",
       "1       80       80     60           1      False  \n",
       "2      100      100     80           1      False  \n",
       "3      122      120     80           1      False  \n",
       "4       60       50     65           1      False  "
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pokemon.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>#</th>\n",
       "      <th>Total</th>\n",
       "      <th>HP</th>\n",
       "      <th>Attack</th>\n",
       "      <th>Defense</th>\n",
       "      <th>Sp. Atk</th>\n",
       "      <th>Sp. Def</th>\n",
       "      <th>Speed</th>\n",
       "      <th>Generation</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>800.000000</td>\n",
       "      <td>800.00000</td>\n",
       "      <td>800.000000</td>\n",
       "      <td>800.000000</td>\n",
       "      <td>800.000000</td>\n",
       "      <td>800.000000</td>\n",
       "      <td>800.000000</td>\n",
       "      <td>800.000000</td>\n",
       "      <td>800.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>362.813750</td>\n",
       "      <td>435.10250</td>\n",
       "      <td>69.258750</td>\n",
       "      <td>79.001250</td>\n",
       "      <td>73.842500</td>\n",
       "      <td>72.820000</td>\n",
       "      <td>71.902500</td>\n",
       "      <td>68.277500</td>\n",
       "      <td>3.32375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>208.343798</td>\n",
       "      <td>119.96304</td>\n",
       "      <td>25.534669</td>\n",
       "      <td>32.457366</td>\n",
       "      <td>31.183501</td>\n",
       "      <td>32.722294</td>\n",
       "      <td>27.828916</td>\n",
       "      <td>29.060474</td>\n",
       "      <td>1.66129</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>180.00000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>1.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>184.750000</td>\n",
       "      <td>330.00000</td>\n",
       "      <td>50.000000</td>\n",
       "      <td>55.000000</td>\n",
       "      <td>50.000000</td>\n",
       "      <td>49.750000</td>\n",
       "      <td>50.000000</td>\n",
       "      <td>45.000000</td>\n",
       "      <td>2.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>364.500000</td>\n",
       "      <td>450.00000</td>\n",
       "      <td>65.000000</td>\n",
       "      <td>75.000000</td>\n",
       "      <td>70.000000</td>\n",
       "      <td>65.000000</td>\n",
       "      <td>70.000000</td>\n",
       "      <td>65.000000</td>\n",
       "      <td>3.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>539.250000</td>\n",
       "      <td>515.00000</td>\n",
       "      <td>80.000000</td>\n",
       "      <td>100.000000</td>\n",
       "      <td>90.000000</td>\n",
       "      <td>95.000000</td>\n",
       "      <td>90.000000</td>\n",
       "      <td>90.000000</td>\n",
       "      <td>5.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>721.000000</td>\n",
       "      <td>780.00000</td>\n",
       "      <td>255.000000</td>\n",
       "      <td>190.000000</td>\n",
       "      <td>230.000000</td>\n",
       "      <td>194.000000</td>\n",
       "      <td>230.000000</td>\n",
       "      <td>180.000000</td>\n",
       "      <td>6.00000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                #      Total          HP      Attack     Defense     Sp. Atk  \\\n",
       "count  800.000000  800.00000  800.000000  800.000000  800.000000  800.000000   \n",
       "mean   362.813750  435.10250   69.258750   79.001250   73.842500   72.820000   \n",
       "std    208.343798  119.96304   25.534669   32.457366   31.183501   32.722294   \n",
       "min      1.000000  180.00000    1.000000    5.000000    5.000000   10.000000   \n",
       "25%    184.750000  330.00000   50.000000   55.000000   50.000000   49.750000   \n",
       "50%    364.500000  450.00000   65.000000   75.000000   70.000000   65.000000   \n",
       "75%    539.250000  515.00000   80.000000  100.000000   90.000000   95.000000   \n",
       "max    721.000000  780.00000  255.000000  190.000000  230.000000  194.000000   \n",
       "\n",
       "          Sp. Def       Speed  Generation  \n",
       "count  800.000000  800.000000   800.00000  \n",
       "mean    71.902500   68.277500     3.32375  \n",
       "std     27.828916   29.060474     1.66129  \n",
       "min     20.000000    5.000000     1.00000  \n",
       "25%     50.000000   45.000000     2.00000  \n",
       "50%     70.000000   65.000000     3.00000  \n",
       "75%     90.000000   90.000000     5.00000  \n",
       "max    230.000000  180.000000     6.00000  "
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pokemon.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据缺失\n",
    "\n",
    "[reference](https://pandas.pydata.org/pandas-docs/stable/missing_data.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>#</th>\n",
       "      <th>Name</th>\n",
       "      <th>Type 1</th>\n",
       "      <th>Type 2</th>\n",
       "      <th>Total</th>\n",
       "      <th>HP</th>\n",
       "      <th>Attack</th>\n",
       "      <th>Defense</th>\n",
       "      <th>Sp. Atk</th>\n",
       "      <th>Sp. Def</th>\n",
       "      <th>Speed</th>\n",
       "      <th>Generation</th>\n",
       "      <th>Legendary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Bulbasaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>318</td>\n",
       "      <td>45</td>\n",
       "      <td>49</td>\n",
       "      <td>49</td>\n",
       "      <td>65</td>\n",
       "      <td>65</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Ivysaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>405</td>\n",
       "      <td>60</td>\n",
       "      <td>62</td>\n",
       "      <td>63</td>\n",
       "      <td>80</td>\n",
       "      <td>80</td>\n",
       "      <td>60</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>525</td>\n",
       "      <td>80</td>\n",
       "      <td>82</td>\n",
       "      <td>83</td>\n",
       "      <td>100</td>\n",
       "      <td>100</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>VenusaurMega Venusaur</td>\n",
       "      <td>Grass</td>\n",
       "      <td>Poison</td>\n",
       "      <td>625</td>\n",
       "      <td>80</td>\n",
       "      <td>100</td>\n",
       "      <td>123</td>\n",
       "      <td>122</td>\n",
       "      <td>120</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>Charmander</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Fire</td>\n",
       "      <td>309</td>\n",
       "      <td>39</td>\n",
       "      <td>52</td>\n",
       "      <td>43</td>\n",
       "      <td>60</td>\n",
       "      <td>50</td>\n",
       "      <td>65</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>Charmeleon</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Fire</td>\n",
       "      <td>405</td>\n",
       "      <td>58</td>\n",
       "      <td>64</td>\n",
       "      <td>58</td>\n",
       "      <td>80</td>\n",
       "      <td>65</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>Charizard</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Flying</td>\n",
       "      <td>534</td>\n",
       "      <td>78</td>\n",
       "      <td>84</td>\n",
       "      <td>78</td>\n",
       "      <td>109</td>\n",
       "      <td>85</td>\n",
       "      <td>100</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6</td>\n",
       "      <td>CharizardMega Charizard X</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Dragon</td>\n",
       "      <td>634</td>\n",
       "      <td>78</td>\n",
       "      <td>130</td>\n",
       "      <td>111</td>\n",
       "      <td>130</td>\n",
       "      <td>85</td>\n",
       "      <td>100</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>6</td>\n",
       "      <td>CharizardMega Charizard Y</td>\n",
       "      <td>Fire</td>\n",
       "      <td>Flying</td>\n",
       "      <td>634</td>\n",
       "      <td>78</td>\n",
       "      <td>104</td>\n",
       "      <td>78</td>\n",
       "      <td>159</td>\n",
       "      <td>115</td>\n",
       "      <td>100</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>7</td>\n",
       "      <td>Squirtle</td>\n",
       "      <td>Water</td>\n",
       "      <td>Water</td>\n",
       "      <td>314</td>\n",
       "      <td>44</td>\n",
       "      <td>48</td>\n",
       "      <td>65</td>\n",
       "      <td>50</td>\n",
       "      <td>64</td>\n",
       "      <td>43</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>8</td>\n",
       "      <td>Wartortle</td>\n",
       "      <td>Water</td>\n",
       "      <td>Water</td>\n",
       "      <td>405</td>\n",
       "      <td>59</td>\n",
       "      <td>63</td>\n",
       "      <td>80</td>\n",
       "      <td>65</td>\n",
       "      <td>80</td>\n",
       "      <td>58</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>9</td>\n",
       "      <td>Blastoise</td>\n",
       "      <td>Water</td>\n",
       "      <td>Water</td>\n",
       "      <td>530</td>\n",
       "      <td>79</td>\n",
       "      <td>83</td>\n",
       "      <td>100</td>\n",
       "      <td>85</td>\n",
       "      <td>105</td>\n",
       "      <td>78</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>9</td>\n",
       "      <td>BlastoiseMega Blastoise</td>\n",
       "      <td>Water</td>\n",
       "      <td>Water</td>\n",
       "      <td>630</td>\n",
       "      <td>79</td>\n",
       "      <td>103</td>\n",
       "      <td>120</td>\n",
       "      <td>135</td>\n",
       "      <td>115</td>\n",
       "      <td>78</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>10</td>\n",
       "      <td>Caterpie</td>\n",
       "      <td>Bug</td>\n",
       "      <td>Bug</td>\n",
       "      <td>195</td>\n",
       "      <td>45</td>\n",
       "      <td>30</td>\n",
       "      <td>35</td>\n",
       "      <td>20</td>\n",
       "      <td>20</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>11</td>\n",
       "      <td>Metapod</td>\n",
       "      <td>Bug</td>\n",
       "      <td>Bug</td>\n",
       "      <td>205</td>\n",
       "      <td>50</td>\n",
       "      <td>20</td>\n",
       "      <td>55</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>30</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>12</td>\n",
       "      <td>Butterfree</td>\n",
       "      <td>Bug</td>\n",
       "      <td>Flying</td>\n",
       "      <td>395</td>\n",
       "      <td>60</td>\n",
       "      <td>45</td>\n",
       "      <td>50</td>\n",
       "      <td>90</td>\n",
       "      <td>80</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>13</td>\n",
       "      <td>Weedle</td>\n",
       "      <td>Bug</td>\n",
       "      <td>Poison</td>\n",
       "      <td>195</td>\n",
       "      <td>40</td>\n",
       "      <td>35</td>\n",
       "      <td>30</td>\n",
       "      <td>20</td>\n",
       "      <td>20</td>\n",
       "      <td>50</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>14</td>\n",
       "      <td>Kakuna</td>\n",
       "      <td>Bug</td>\n",
       "      <td>Poison</td>\n",
       "      <td>205</td>\n",
       "      <td>45</td>\n",
       "      <td>25</td>\n",
       "      <td>50</td>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>35</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>15</td>\n",
       "      <td>Beedrill</td>\n",
       "      <td>Bug</td>\n",
       "      <td>Poison</td>\n",
       "      <td>395</td>\n",
       "      <td>65</td>\n",
       "      <td>90</td>\n",
       "      <td>40</td>\n",
       "      <td>45</td>\n",
       "      <td>80</td>\n",
       "      <td>75</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>15</td>\n",
       "      <td>BeedrillMega Beedrill</td>\n",
       "      <td>Bug</td>\n",
       "      <td>Poison</td>\n",
       "      <td>495</td>\n",
       "      <td>65</td>\n",
       "      <td>150</td>\n",
       "      <td>40</td>\n",
       "      <td>15</td>\n",
       "      <td>80</td>\n",
       "      <td>145</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>16</td>\n",
       "      <td>Pidgey</td>\n",
       "      <td>Normal</td>\n",
       "      <td>Flying</td>\n",
       "      <td>251</td>\n",
       "      <td>40</td>\n",
       "      <td>45</td>\n",
       "      <td>40</td>\n",
       "      <td>35</td>\n",
       "      <td>35</td>\n",
       "      <td>56</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>17</td>\n",
       "      <td>Pidgeotto</td>\n",
       "      <td>Normal</td>\n",
       "      <td>Flying</td>\n",
       "      <td>349</td>\n",
       "      <td>63</td>\n",
       "      <td>60</td>\n",
       "      <td>55</td>\n",
       "      <td>50</td>\n",
       "      <td>50</td>\n",
       "      <td>71</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>18</td>\n",
       "      <td>Pidgeot</td>\n",
       "      <td>Normal</td>\n",
       "      <td>Flying</td>\n",
       "      <td>479</td>\n",
       "      <td>83</td>\n",
       "      <td>80</td>\n",
       "      <td>75</td>\n",
       "      <td>70</td>\n",
       "      <td>70</td>\n",
       "      <td>101</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>18</td>\n",
       "      <td>PidgeotMega Pidgeot</td>\n",
       "      <td>Normal</td>\n",
       "      <td>Flying</td>\n",
       "      <td>579</td>\n",
       "      <td>83</td>\n",
       "      <td>80</td>\n",
       "      <td>80</td>\n",
       "      <td>135</td>\n",
       "      <td>80</td>\n",
       "      <td>121</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>19</td>\n",
       "      <td>Rattata</td>\n",
       "      <td>Normal</td>\n",
       "      <td>Normal</td>\n",
       "      <td>253</td>\n",
       "      <td>30</td>\n",
       "      <td>56</td>\n",
       "      <td>35</td>\n",
       "      <td>25</td>\n",
       "      <td>35</td>\n",
       "      <td>72</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>20</td>\n",
       "      <td>Raticate</td>\n",
       "      <td>Normal</td>\n",
       "      <td>Normal</td>\n",
       "      <td>413</td>\n",
       "      <td>55</td>\n",
       "      <td>81</td>\n",
       "      <td>60</td>\n",
       "      <td>50</td>\n",
       "      <td>70</td>\n",
       "      <td>97</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>21</td>\n",
       "      <td>Spearow</td>\n",
       "      <td>Normal</td>\n",
       "      <td>Flying</td>\n",
       "      <td>262</td>\n",
       "      <td>40</td>\n",
       "      <td>60</td>\n",
       "      <td>30</td>\n",
       "      <td>31</td>\n",
       "      <td>31</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>22</td>\n",
       "      <td>Fearow</td>\n",
       "      <td>Normal</td>\n",
       "      <td>Flying</td>\n",
       "      <td>442</td>\n",
       "      <td>65</td>\n",
       "      <td>90</td>\n",
       "      <td>65</td>\n",
       "      <td>61</td>\n",
       "      <td>61</td>\n",
       "      <td>100</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>23</td>\n",
       "      <td>Ekans</td>\n",
       "      <td>Poison</td>\n",
       "      <td>Poison</td>\n",
       "      <td>288</td>\n",
       "      <td>35</td>\n",
       "      <td>60</td>\n",
       "      <td>44</td>\n",
       "      <td>40</td>\n",
       "      <td>54</td>\n",
       "      <td>55</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>24</td>\n",
       "      <td>Arbok</td>\n",
       "      <td>Poison</td>\n",
       "      <td>Poison</td>\n",
       "      <td>438</td>\n",
       "      <td>60</td>\n",
       "      <td>85</td>\n",
       "      <td>69</td>\n",
       "      <td>65</td>\n",
       "      <td>79</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>25</td>\n",
       "      <td>Pikachu</td>\n",
       "      <td>Electric</td>\n",
       "      <td>Electric</td>\n",
       "      <td>320</td>\n",
       "      <td>35</td>\n",
       "      <td>55</td>\n",
       "      <td>40</td>\n",
       "      <td>50</td>\n",
       "      <td>50</td>\n",
       "      <td>90</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>26</td>\n",
       "      <td>Raichu</td>\n",
       "      <td>Electric</td>\n",
       "      <td>Electric</td>\n",
       "      <td>485</td>\n",
       "      <td>60</td>\n",
       "      <td>90</td>\n",
       "      <td>55</td>\n",
       "      <td>90</td>\n",
       "      <td>80</td>\n",
       "      <td>110</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>27</td>\n",
       "      <td>Sandshrew</td>\n",
       "      <td>Ground</td>\n",
       "      <td>Ground</td>\n",
       "      <td>300</td>\n",
       "      <td>50</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>40</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>28</td>\n",
       "      <td>Sandslash</td>\n",
       "      <td>Ground</td>\n",
       "      <td>Ground</td>\n",
       "      <td>450</td>\n",
       "      <td>75</td>\n",
       "      <td>100</td>\n",
       "      <td>110</td>\n",
       "      <td>45</td>\n",
       "      <td>55</td>\n",
       "      <td>65</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>29</td>\n",
       "      <td>Nidoran♀</td>\n",
       "      <td>Poison</td>\n",
       "      <td>Poison</td>\n",
       "      <td>275</td>\n",
       "      <td>55</td>\n",
       "      <td>47</td>\n",
       "      <td>52</td>\n",
       "      <td>40</td>\n",
       "      <td>40</td>\n",
       "      <td>41</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>30</td>\n",
       "      <td>Nidorina</td>\n",
       "      <td>Poison</td>\n",
       "      <td>Poison</td>\n",
       "      <td>365</td>\n",
       "      <td>70</td>\n",
       "      <td>62</td>\n",
       "      <td>67</td>\n",
       "      <td>55</td>\n",
       "      <td>55</td>\n",
       "      <td>56</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>31</td>\n",
       "      <td>Nidoqueen</td>\n",
       "      <td>Poison</td>\n",
       "      <td>Ground</td>\n",
       "      <td>505</td>\n",
       "      <td>90</td>\n",
       "      <td>92</td>\n",
       "      <td>87</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>76</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>32</td>\n",
       "      <td>Nidoran♂</td>\n",
       "      <td>Poison</td>\n",
       "      <td>Poison</td>\n",
       "      <td>273</td>\n",
       "      <td>46</td>\n",
       "      <td>57</td>\n",
       "      <td>40</td>\n",
       "      <td>40</td>\n",
       "      <td>40</td>\n",
       "      <td>50</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>33</td>\n",
       "      <td>Nidorino</td>\n",
       "      <td>Poison</td>\n",
       "      <td>Poison</td>\n",
       "      <td>365</td>\n",
       "      <td>61</td>\n",
       "      <td>72</td>\n",
       "      <td>57</td>\n",
       "      <td>55</td>\n",
       "      <td>55</td>\n",
       "      <td>65</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>34</td>\n",
       "      <td>Nidoking</td>\n",
       "      <td>Poison</td>\n",
       "      <td>Ground</td>\n",
       "      <td>505</td>\n",
       "      <td>81</td>\n",
       "      <td>102</td>\n",
       "      <td>77</td>\n",
       "      <td>85</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     #                       Name    Type 1    Type 2  Total  HP  Attack  \\\n",
       "0    1                  Bulbasaur     Grass    Poison    318  45      49   \n",
       "1    2                    Ivysaur     Grass    Poison    405  60      62   \n",
       "2    3                   Venusaur     Grass    Poison    525  80      82   \n",
       "3    3      VenusaurMega Venusaur     Grass    Poison    625  80     100   \n",
       "4    4                 Charmander      Fire      Fire    309  39      52   \n",
       "5    5                 Charmeleon      Fire      Fire    405  58      64   \n",
       "6    6                  Charizard      Fire    Flying    534  78      84   \n",
       "7    6  CharizardMega Charizard X      Fire    Dragon    634  78     130   \n",
       "8    6  CharizardMega Charizard Y      Fire    Flying    634  78     104   \n",
       "9    7                   Squirtle     Water     Water    314  44      48   \n",
       "10   8                  Wartortle     Water     Water    405  59      63   \n",
       "11   9                  Blastoise     Water     Water    530  79      83   \n",
       "12   9    BlastoiseMega Blastoise     Water     Water    630  79     103   \n",
       "13  10                   Caterpie       Bug       Bug    195  45      30   \n",
       "14  11                    Metapod       Bug       Bug    205  50      20   \n",
       "15  12                 Butterfree       Bug    Flying    395  60      45   \n",
       "16  13                     Weedle       Bug    Poison    195  40      35   \n",
       "17  14                     Kakuna       Bug    Poison    205  45      25   \n",
       "18  15                   Beedrill       Bug    Poison    395  65      90   \n",
       "19  15      BeedrillMega Beedrill       Bug    Poison    495  65     150   \n",
       "20  16                     Pidgey    Normal    Flying    251  40      45   \n",
       "21  17                  Pidgeotto    Normal    Flying    349  63      60   \n",
       "22  18                    Pidgeot    Normal    Flying    479  83      80   \n",
       "23  18        PidgeotMega Pidgeot    Normal    Flying    579  83      80   \n",
       "24  19                    Rattata    Normal    Normal    253  30      56   \n",
       "25  20                   Raticate    Normal    Normal    413  55      81   \n",
       "26  21                    Spearow    Normal    Flying    262  40      60   \n",
       "27  22                     Fearow    Normal    Flying    442  65      90   \n",
       "28  23                      Ekans    Poison    Poison    288  35      60   \n",
       "29  24                      Arbok    Poison    Poison    438  60      85   \n",
       "30  25                    Pikachu  Electric  Electric    320  35      55   \n",
       "31  26                     Raichu  Electric  Electric    485  60      90   \n",
       "32  27                  Sandshrew    Ground    Ground    300  50      75   \n",
       "33  28                  Sandslash    Ground    Ground    450  75     100   \n",
       "34  29                   Nidoran♀    Poison    Poison    275  55      47   \n",
       "35  30                   Nidorina    Poison    Poison    365  70      62   \n",
       "36  31                  Nidoqueen    Poison    Ground    505  90      92   \n",
       "37  32                   Nidoran♂    Poison    Poison    273  46      57   \n",
       "38  33                   Nidorino    Poison    Poison    365  61      72   \n",
       "39  34                   Nidoking    Poison    Ground    505  81     102   \n",
       "\n",
       "    Defense  Sp. Atk  Sp. Def  Speed  Generation  Legendary  \n",
       "0        49       65       65     45           1      False  \n",
       "1        63       80       80     60           1      False  \n",
       "2        83      100      100     80           1      False  \n",
       "3       123      122      120     80           1      False  \n",
       "4        43       60       50     65           1      False  \n",
       "5        58       80       65     80           1      False  \n",
       "6        78      109       85    100           1      False  \n",
       "7       111      130       85    100           1      False  \n",
       "8        78      159      115    100           1      False  \n",
       "9        65       50       64     43           1      False  \n",
       "10       80       65       80     58           1      False  \n",
       "11      100       85      105     78           1      False  \n",
       "12      120      135      115     78           1      False  \n",
       "13       35       20       20     45           1      False  \n",
       "14       55       25       25     30           1      False  \n",
       "15       50       90       80     70           1      False  \n",
       "16       30       20       20     50           1      False  \n",
       "17       50       25       25     35           1      False  \n",
       "18       40       45       80     75           1      False  \n",
       "19       40       15       80    145           1      False  \n",
       "20       40       35       35     56           1      False  \n",
       "21       55       50       50     71           1      False  \n",
       "22       75       70       70    101           1      False  \n",
       "23       80      135       80    121           1      False  \n",
       "24       35       25       35     72           1      False  \n",
       "25       60       50       70     97           1      False  \n",
       "26       30       31       31     70           1      False  \n",
       "27       65       61       61    100           1      False  \n",
       "28       44       40       54     55           1      False  \n",
       "29       69       65       79     80           1      False  \n",
       "30       40       50       50     90           1      False  \n",
       "31       55       90       80    110           1      False  \n",
       "32       85       20       30     40           1      False  \n",
       "33      110       45       55     65           1      False  \n",
       "34       52       40       40     41           1      False  \n",
       "35       67       55       55     56           1      False  \n",
       "36       87       75       85     76           1      False  \n",
       "37       40       40       40     50           1      False  \n",
       "38       57       55       55     65           1      False  \n",
       "39       77       85       75     85           1      False  "
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pokemon.loc[pokemon[\"Type 2\"].isnull(), \"Type 2\"] = pokemon.loc[pokemon[\"Type 2\"].isnull(), \"Type 1\"]\n",
    "pokemon.head(40)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print('Hangzhou' in apts)\n",
    "print('Hangzhou' in cars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Beijing       True\n",
       "Shanghai      True\n",
       "Shenzhen      True\n",
       "Hangzhou      True\n",
       "Guangzhou     True\n",
       "Suzhou       False\n",
       "Name: price, dtype: bool"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apts.notnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Beijing      False\n",
      "Shanghai     False\n",
      "Shenzhen     False\n",
      "Hangzhou     False\n",
      "Guangzhou    False\n",
      "Suzhou        True\n",
      "Name: price, dtype: bool\n"
     ]
    }
   ],
   "source": [
    "print(apts.isnull())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Suzhou   NaN\n",
      "Name: price, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(apts[apts.isnull()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Beijing      55000.0\n",
      "Shanghai     60000.0\n",
      "Shenzhen     55000.0\n",
      "Hangzhou     40000.0\n",
      "Guangzhou    40000.0\n",
      "Name: price, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(apts[apts.isnull() == False])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用to_csv方法写出到文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.795114</td>\n",
       "      <td>0.577349</td>\n",
       "      <td>0.301719</td>\n",
       "      <td>0.030007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.447010</td>\n",
       "      <td>0.763519</td>\n",
       "      <td>0.413005</td>\n",
       "      <td>0.650911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.782053</td>\n",
       "      <td>0.540552</td>\n",
       "      <td>0.847665</td>\n",
       "      <td>0.493705</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.443028</td>\n",
       "      <td>0.171188</td>\n",
       "      <td>0.882141</td>\n",
       "      <td>0.061379</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.832740</td>\n",
       "      <td>0.525958</td>\n",
       "      <td>0.813365</td>\n",
       "      <td>0.267642</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.346607</td>\n",
       "      <td>0.095022</td>\n",
       "      <td>0.637025</td>\n",
       "      <td>0.369896</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.594667</td>\n",
       "      <td>0.008349</td>\n",
       "      <td>0.395650</td>\n",
       "      <td>0.806280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.013310</td>\n",
       "      <td>0.562958</td>\n",
       "      <td>0.535221</td>\n",
       "      <td>0.546262</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.711791</td>\n",
       "      <td>0.584724</td>\n",
       "      <td>0.031253</td>\n",
       "      <td>0.256998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.308172</td>\n",
       "      <td>0.772345</td>\n",
       "      <td>0.809047</td>\n",
       "      <td>0.638079</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          a         b         c         d\n",
       "0  0.795114  0.577349  0.301719  0.030007\n",
       "1  0.447010  0.763519  0.413005  0.650911\n",
       "2  0.782053  0.540552  0.847665  0.493705\n",
       "3  0.443028  0.171188  0.882141  0.061379\n",
       "4  0.832740  0.525958  0.813365  0.267642\n",
       "5  0.346607  0.095022  0.637025  0.369896\n",
       "6  0.594667  0.008349  0.395650  0.806280\n",
       "7  0.013310  0.562958  0.535221  0.546262\n",
       "8  0.711791  0.584724  0.031253  0.256998\n",
       "9  0.308172  0.772345  0.809047  0.638079"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.rand(10, 4), columns=list(\"abcd\"))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df.to_csv(\"data/sample.tsv\", sep=\"\\t\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10.795114</td>\n",
       "      <td>10.577349</td>\n",
       "      <td>10.301719</td>\n",
       "      <td>10.030007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10.447010</td>\n",
       "      <td>10.763519</td>\n",
       "      <td>10.413005</td>\n",
       "      <td>10.650911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.782053</td>\n",
       "      <td>10.540552</td>\n",
       "      <td>10.847665</td>\n",
       "      <td>10.493705</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10.443028</td>\n",
       "      <td>10.171188</td>\n",
       "      <td>10.882141</td>\n",
       "      <td>10.061379</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10.832740</td>\n",
       "      <td>10.525958</td>\n",
       "      <td>10.813365</td>\n",
       "      <td>10.267642</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>10.346607</td>\n",
       "      <td>10.095022</td>\n",
       "      <td>10.637025</td>\n",
       "      <td>10.369896</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>10.594667</td>\n",
       "      <td>10.008349</td>\n",
       "      <td>10.395650</td>\n",
       "      <td>10.806280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>10.013310</td>\n",
       "      <td>10.562958</td>\n",
       "      <td>10.535221</td>\n",
       "      <td>10.546262</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>10.711791</td>\n",
       "      <td>10.584724</td>\n",
       "      <td>10.031253</td>\n",
       "      <td>10.256998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10.308172</td>\n",
       "      <td>10.772345</td>\n",
       "      <td>10.809047</td>\n",
       "      <td>10.638079</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           a          b          c          d\n",
       "0  10.795114  10.577349  10.301719  10.030007\n",
       "1  10.447010  10.763519  10.413005  10.650911\n",
       "2  10.782053  10.540552  10.847665  10.493705\n",
       "3  10.443028  10.171188  10.882141  10.061379\n",
       "4  10.832740  10.525958  10.813365  10.267642\n",
       "5  10.346607  10.095022  10.637025  10.369896\n",
       "6  10.594667  10.008349  10.395650  10.806280\n",
       "7  10.013310  10.562958  10.535221  10.546262\n",
       "8  10.711791  10.584724  10.031253  10.256998\n",
       "9  10.308172  10.772345  10.809047  10.638079"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.add(pd.Series(np.ones(10)) * 10, axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Index\n",
    "### 2018七月在线 机器学习集训营第六期  julyedu.com"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### index object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['a', 'b', 'c'], dtype='object')\n",
      "Index(['b', 'c'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "obj = pd.Series(range(3), index = ['a', 'b', 'c'])\n",
    "index = obj.index\n",
    "print(index)\n",
    "print(index[1:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "index的值是不能被更改的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# index[1] = 'd'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 针对index进行索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "obj = pd.Series(np.arange(4), index=['a', 'b', 'c', 'd'])\n",
    "print(obj['b'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "默认的数字index依旧可以使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "\n",
      "b    1\n",
      "d    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print(obj[3])\n",
    "print()\n",
    "print(obj[[1,3]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面介绍如何对Series进行切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "a    0\n",
      "b    5\n",
      "c    5\n",
      "d    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print(obj['b':'c'])\n",
    "obj['b':'c'] = 5\n",
    "print(obj)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对DataFrame进行Indexing与Series基本相同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "information  year       city  population   debt  western\n",
      "index                                                   \n",
      "one          2016    Beijing        2200    NaN    False\n",
      "two          2017   Shanghai        2300  100.0    False\n",
      "three        2016  Guangzhou        1000  200.0    False\n",
      "four         2017   Shenzhen         700    NaN    False\n",
      "five         2016   Hangzhou         500  300.0    False\n",
      "six             0          0           0    NaN    False\n"
     ]
    }
   ],
   "source": [
    "print(frame)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "information  year      city  population   debt  western\n",
      "index                                                  \n",
      "one          2016   Beijing        2200    NaN    False\n",
      "two          2017  Shanghai        2300  100.0    False\n"
     ]
    }
   ],
   "source": [
    "print(frame[:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "information\n",
      "year              2017\n",
      "city          Shanghai\n",
      "population        2300\n",
      "debt               100\n",
      "western          False\n",
      "Name: two, dtype: object\n"
     ]
    }
   ],
   "source": [
    "print(frame.loc['two'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "information      city  population\n",
      "index                            \n",
      "two          Shanghai        2300\n",
      "four         Shenzhen         700\n"
     ]
    }
   ],
   "source": [
    "print(frame.loc[[\"two\", \"four\"], ['city', 'population']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "index\n",
      "one        Beijing\n",
      "two       Shanghai\n",
      "three    Guangzhou\n",
      "four      Shenzhen\n",
      "Name: city, dtype: object\n"
     ]
    }
   ],
   "source": [
    "print(frame.loc[:'four', 'city'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [reindex](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reindex.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "把一个Series或者DataFrame按照新的index顺序进行重排"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d    4.5\n",
      "b    7.2\n",
      "a   -5.3\n",
      "c    3.2\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "obj = pd.Series([4.5, 7.2, -5.3, 3.2], index=['d', 'b', 'a', 'c'])\n",
    "print(obj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a   -5.3\n",
      "b    7.2\n",
      "c    3.2\n",
      "d    4.5\n",
      "e    NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "obj2 = obj.reindex(['a', 'b', 'c', 'd', 'e'])\n",
    "print(obj2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a   -5.3\n",
      "b    7.2\n",
      "c    3.2\n",
      "d    4.5\n",
      "e    0.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(obj.reindex(['a', 'b', 'c', 'd', 'e'], fill_value = 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0      blue\n",
      "2    purple\n",
      "4    yellow\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "obj3 = pd.Series(['blue', 'purple', 'yellow'], index = [0,2,4])\n",
    "print(obj3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果我们reindex的index长度比原来的index长，可以指定方法来fill NaN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0      blue\n",
      "1      blue\n",
      "2    purple\n",
      "3    purple\n",
      "4    yellow\n",
      "5    yellow\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "print(obj3.reindex(range(6), method='ffill'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0      blue\n",
      "1    purple\n",
      "2    purple\n",
      "3    yellow\n",
      "4    yellow\n",
      "5       NaN\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "print(obj3.reindex(range(6), method='bfill'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "既然我们可以对Series进行reindex，相应地，我们也可以用同样的方法对DataFrame进行reindex。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在reindex的同时，我们还可以重新指定columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>information</th>\n",
       "      <th>year</th>\n",
       "      <th>city</th>\n",
       "      <th>population</th>\n",
       "      <th>debt</th>\n",
       "      <th>western</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>index</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>2016</td>\n",
       "      <td>Beijing</td>\n",
       "      <td>2200</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>2017</td>\n",
       "      <td>Shanghai</td>\n",
       "      <td>2300</td>\n",
       "      <td>100.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>2016</td>\n",
       "      <td>Guangzhou</td>\n",
       "      <td>1000</td>\n",
       "      <td>200.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>four</th>\n",
       "      <td>2017</td>\n",
       "      <td>Shenzhen</td>\n",
       "      <td>700</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>five</th>\n",
       "      <td>2016</td>\n",
       "      <td>Hangzhou</td>\n",
       "      <td>500</td>\n",
       "      <td>300.0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>six</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "information  year       city  population   debt  western\n",
       "index                                                   \n",
       "one          2016    Beijing        2200    NaN    False\n",
       "two          2017   Shanghai        2300  100.0    False\n",
       "three        2016  Guangzhou        1000  200.0    False\n",
       "four         2017   Shenzhen         700    NaN    False\n",
       "five         2016   Hangzhou         500  300.0    False\n",
       "six             0          0           0    NaN    False"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "information       city  year  population  western   debt\n",
      "index                                                   \n",
      "one            Beijing  2016        2200    False    NaN\n",
      "two           Shanghai  2017        2300    False  100.0\n",
      "three        Guangzhou  2016        1000    False  200.0\n",
      "four          Shenzhen  2017         700    False    NaN\n",
      "five          Hangzhou  2016         500    False  300.0\n",
      "six                  0     0           0    False    NaN\n"
     ]
    }
   ],
   "source": [
    "print(frame.reindex(columns = ['city', 'year', 'population', \"western\", \"debt\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "information       city  year  population\n",
      "index                                   \n",
      "two           Shanghai  2017        2300\n",
      "four          Shenzhen  2017         700\n",
      "three        Guangzhou  2016        1000\n",
      "one            Beijing  2016        2200\n"
     ]
    }
   ],
   "source": [
    "print(frame.reindex(index = [\"two\", \"four\", \"three\", \"one\"],\n",
    "                    columns = ['city', 'year', 'population']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面介绍如何用drop来删除Series和DataFrame中的index，注意drop的效果不是in place的，也就是说他会返回一个object，原来的Obejct并没有被改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0      blue\n",
      "2    purple\n",
      "4    yellow\n",
      "dtype: object\n",
      "0      blue\n",
      "4    yellow\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "print(obj3)\n",
    "obj4 = obj3.drop(2)\n",
    "print(obj4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    blue\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "print(obj3.drop([2,4]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "information  year       city  population   debt  western\n",
      "index                                                   \n",
      "one          2016    Beijing        2200    NaN    False\n",
      "two          2017   Shanghai        2300  100.0    False\n",
      "three        2016  Guangzhou        1000  200.0    False\n",
      "four         2017   Shenzhen         700    NaN    False\n",
      "five         2016   Hangzhou         500  300.0    False\n",
      "six             0          0           0    NaN    False\n"
     ]
    }
   ],
   "source": [
    "print(frame)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "information  year       city  population   debt  western\n",
      "index                                                   \n",
      "three        2016  Guangzhou        1000  200.0    False\n",
      "four         2017   Shenzhen         700    NaN    False\n",
      "five         2016   Hangzhou         500  300.0    False\n",
      "six             0          0           0    NaN    False\n"
     ]
    }
   ],
   "source": [
    "print(frame.drop([\"one\", \"two\"]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "drop不仅仅可以删除行，还可以删除列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "information  year       city  population   debt\n",
      "index                                          \n",
      "one          2016    Beijing        2200    NaN\n",
      "two          2017   Shanghai        2300  100.0\n",
      "three        2016  Guangzhou        1000  200.0\n",
      "four         2017   Shenzhen         700    NaN\n",
      "five         2016   Hangzhou         500  300.0\n",
      "six             0          0           0    NaN\n"
     ]
    }
   ],
   "source": [
    "print(frame.drop('western', axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "information  year       city  population\n",
      "index                                   \n",
      "one          2016    Beijing        2200\n",
      "two          2017   Shanghai        2300\n",
      "three        2016  Guangzhou        1000\n",
      "four         2017   Shenzhen         700\n",
      "five         2016   Hangzhou         500\n",
      "six             0          0           0\n"
     ]
    }
   ],
   "source": [
    "print(frame.drop(['western', 'debt'], axis=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 总结练习\n",
    "### 2018七月在线 机器学习集训营第六期 julyedu.com\n",
    "by 褚则伟 zeweichu@gmail.com"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Adj Close</th>\n",
       "      <th>Volume</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2004-08-19</td>\n",
       "      <td>49.813286</td>\n",
       "      <td>51.835709</td>\n",
       "      <td>47.800831</td>\n",
       "      <td>49.982655</td>\n",
       "      <td>49.982655</td>\n",
       "      <td>44871300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2004-08-20</td>\n",
       "      <td>50.316402</td>\n",
       "      <td>54.336334</td>\n",
       "      <td>50.062355</td>\n",
       "      <td>53.952770</td>\n",
       "      <td>53.952770</td>\n",
       "      <td>22942800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2004-08-23</td>\n",
       "      <td>55.168217</td>\n",
       "      <td>56.528118</td>\n",
       "      <td>54.321388</td>\n",
       "      <td>54.495735</td>\n",
       "      <td>54.495735</td>\n",
       "      <td>18342800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2004-08-24</td>\n",
       "      <td>55.412300</td>\n",
       "      <td>55.591629</td>\n",
       "      <td>51.591621</td>\n",
       "      <td>52.239193</td>\n",
       "      <td>52.239193</td>\n",
       "      <td>15319700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2004-08-25</td>\n",
       "      <td>52.284027</td>\n",
       "      <td>53.798351</td>\n",
       "      <td>51.746044</td>\n",
       "      <td>52.802086</td>\n",
       "      <td>52.802086</td>\n",
       "      <td>9232100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Date       Open       High        Low      Close  Adj Close    Volume\n",
       "0  2004-08-19  49.813286  51.835709  47.800831  49.982655  49.982655  44871300\n",
       "1  2004-08-20  50.316402  54.336334  50.062355  53.952770  53.952770  22942800\n",
       "2  2004-08-23  55.168217  56.528118  54.321388  54.495735  54.495735  18342800\n",
       "3  2004-08-24  55.412300  55.591629  51.591621  52.239193  52.239193  15319700\n",
       "4  2004-08-25  52.284027  53.798351  51.746044  52.802086  52.802086   9232100"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "goog = pd.read_csv(\"data/GOOG.csv\")\n",
    "goog.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                 Open       High        Low      Close  Adj Close    Volume\n",
      "Date                                                                       \n",
      "2004-08-19  49.813286  51.835709  47.800831  49.982655  49.982655  44871300\n",
      "2004-08-20  50.316402  54.336334  50.062355  53.952770  53.952770  22942800\n",
      "2004-08-23  55.168217  56.528118  54.321388  54.495735  54.495735  18342800\n",
      "2004-08-24  55.412300  55.591629  51.591621  52.239193  52.239193  15319700\n",
      "2004-08-25  52.284027  53.798351  51.746044  52.802086  52.802086   9232100\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Index(['2004-08-19', '2004-08-20', '2004-08-23', '2004-08-24', '2004-08-25',\n",
       "       '2004-08-26', '2004-08-27', '2004-08-30', '2004-08-31', '2004-09-01',\n",
       "       ...\n",
       "       '2017-07-07', '2017-07-10', '2017-07-11', '2017-07-12', '2017-07-13',\n",
       "       '2017-07-14', '2017-07-17', '2017-07-18', '2017-07-19', '2017-07-20'],\n",
       "      dtype='object', name='Date', length=3253)"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "goog = pd.read_csv(\"data/GOOG.csv\", index_col=0)\n",
    "print(goog.head())\n",
    "goog.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2004-08-19', '2004-08-20', '2004-08-23', '2004-08-24',\n",
       "               '2004-08-25', '2004-08-26', '2004-08-27', '2004-08-30',\n",
       "               '2004-08-31', '2004-09-01',\n",
       "               ...\n",
       "               '2017-07-07', '2017-07-10', '2017-07-11', '2017-07-12',\n",
       "               '2017-07-13', '2017-07-14', '2017-07-17', '2017-07-18',\n",
       "               '2017-07-19', '2017-07-20'],\n",
       "              dtype='datetime64[ns]', name='Date', length=3253, freq=None)"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "goog = goog.reindex(pd.to_datetime(goog.index))\n",
    "goog.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2004-08-19', '2004-08-20', '2004-08-23', '2004-08-24',\n",
       "               '2004-08-25', '2004-08-26', '2004-08-27', '2004-08-30',\n",
       "               '2004-08-31', '2004-09-01',\n",
       "               ...\n",
       "               '2017-07-07', '2017-07-10', '2017-07-11', '2017-07-12',\n",
       "               '2017-07-13', '2017-07-14', '2017-07-17', '2017-07-18',\n",
       "               '2017-07-19', '2017-07-20'],\n",
       "              dtype='datetime64[ns]', name='Date', length=3253, freq=None)"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "goog = pd.read_csv(\"data/GOOG.csv\", index_col=0, parse_dates=[0])\n",
    "goog.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Adj Close</th>\n",
       "      <th>Volume</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2004-08-19</th>\n",
       "      <td>49.813286</td>\n",
       "      <td>51.835709</td>\n",
       "      <td>47.800831</td>\n",
       "      <td>49.982655</td>\n",
       "      <td>49.982655</td>\n",
       "      <td>44871300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-08-20</th>\n",
       "      <td>50.316402</td>\n",
       "      <td>54.336334</td>\n",
       "      <td>50.062355</td>\n",
       "      <td>53.952770</td>\n",
       "      <td>53.952770</td>\n",
       "      <td>22942800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-08-23</th>\n",
       "      <td>55.168217</td>\n",
       "      <td>56.528118</td>\n",
       "      <td>54.321388</td>\n",
       "      <td>54.495735</td>\n",
       "      <td>54.495735</td>\n",
       "      <td>18342800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-08-24</th>\n",
       "      <td>55.412300</td>\n",
       "      <td>55.591629</td>\n",
       "      <td>51.591621</td>\n",
       "      <td>52.239193</td>\n",
       "      <td>52.239193</td>\n",
       "      <td>15319700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-08-25</th>\n",
       "      <td>52.284027</td>\n",
       "      <td>53.798351</td>\n",
       "      <td>51.746044</td>\n",
       "      <td>52.802086</td>\n",
       "      <td>52.802086</td>\n",
       "      <td>9232100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 Open       High        Low      Close  Adj Close    Volume\n",
       "Date                                                                       \n",
       "2004-08-19  49.813286  51.835709  47.800831  49.982655  49.982655  44871300\n",
       "2004-08-20  50.316402  54.336334  50.062355  53.952770  53.952770  22942800\n",
       "2004-08-23  55.168217  56.528118  54.321388  54.495735  54.495735  18342800\n",
       "2004-08-24  55.412300  55.591629  51.591621  52.239193  52.239193  15319700\n",
       "2004-08-25  52.284027  53.798351  51.746044  52.802086  52.802086   9232100"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "goog.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Adj Close</th>\n",
       "      <th>Volume</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017-07-14</th>\n",
       "      <td>952.000000</td>\n",
       "      <td>956.909973</td>\n",
       "      <td>948.005005</td>\n",
       "      <td>955.989990</td>\n",
       "      <td>955.989990</td>\n",
       "      <td>1053800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-07-17</th>\n",
       "      <td>957.000000</td>\n",
       "      <td>960.739990</td>\n",
       "      <td>949.241028</td>\n",
       "      <td>953.419983</td>\n",
       "      <td>953.419983</td>\n",
       "      <td>1165500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-07-18</th>\n",
       "      <td>953.000000</td>\n",
       "      <td>968.039978</td>\n",
       "      <td>950.599976</td>\n",
       "      <td>965.400024</td>\n",
       "      <td>965.400024</td>\n",
       "      <td>1154000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-07-19</th>\n",
       "      <td>967.840027</td>\n",
       "      <td>973.039978</td>\n",
       "      <td>964.030029</td>\n",
       "      <td>970.890015</td>\n",
       "      <td>970.890015</td>\n",
       "      <td>1224500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-07-20</th>\n",
       "      <td>975.000000</td>\n",
       "      <td>975.900024</td>\n",
       "      <td>961.510010</td>\n",
       "      <td>968.150024</td>\n",
       "      <td>968.150024</td>\n",
       "      <td>1616500</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  Open        High         Low       Close   Adj Close  \\\n",
       "Date                                                                     \n",
       "2017-07-14  952.000000  956.909973  948.005005  955.989990  955.989990   \n",
       "2017-07-17  957.000000  960.739990  949.241028  953.419983  953.419983   \n",
       "2017-07-18  953.000000  968.039978  950.599976  965.400024  965.400024   \n",
       "2017-07-19  967.840027  973.039978  964.030029  970.890015  970.890015   \n",
       "2017-07-20  975.000000  975.900024  961.510010  968.150024  968.150024   \n",
       "\n",
       "             Volume  \n",
       "Date                 \n",
       "2017-07-14  1053800  \n",
       "2017-07-17  1165500  \n",
       "2017-07-18  1154000  \n",
       "2017-07-19  1224500  \n",
       "2017-07-20  1616500  "
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "goog.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x1101a40b8>"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAEACAYAAAC6d6FnAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAA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\niKwD3gdexOoZtNpePSoLuLi+vgGlKksbgZVSKkxpFZBSSoUpTQBKKRWmNAEopVSY0gSglFJhShOA\nUkqFKU0ASikVpjQBKKVUmNIEoJRSYer/ATI+dH1ee/LFAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x10eef2588>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "goog[\"Adj Close\"].plot()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
